﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Store.DateModel;
using System.Web.UI.HtmlControls;
public partial class Pages_WebControls_Oferta : System.Web.UI.UserControl
{
    private StoreApplicationService sas = new StoreApplicationService();
    private int _NumberOfPromotions = 5;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public string NumberOfPromotions
    {
        get { return _NumberOfPromotions + ""; }
        set { _NumberOfPromotions = Convert.ToInt32(value); }
    }


    protected void OfertaContentMV_Init(object sender, EventArgs e)
    {
        var promotii = sas.GetPromotionProducts(_NumberOfPromotions);
        int i = 1;
        foreach (ProductModel prom in promotii)
        {
            var oferta_content = new HtmlGenericControl("div");
            oferta_content.Attributes["class"] = "oferta_content";
            {
                var oferta_img = new HtmlGenericControl("img");
                oferta_img.Attributes["src"] = Page.ResolveUrl(prom.image_url);
                oferta_img.Attributes["width"] = "94";
                oferta_img.Attributes["height"] = "92";
                oferta_img.Attributes["border"] = "0";
                oferta_img.Attributes["class"] = "oferta_img";
                oferta_content.Controls.Add(oferta_img);

                var oferta_details = new HtmlGenericControl("div");
                oferta_details.Attributes["class"] = "oferta_details";
                {
                    var oferta_title = new HtmlGenericControl("div");
                    oferta_title.Attributes["class"] = "oferta_title";
                    oferta_title.Controls.Add(new Label() { Text = prom.name });

                    var oferta_text = new HtmlGenericControl("div");
                    oferta_text.Attributes["class"] = "oferta_text";
                    {
                        var prod_details = new HtmlGenericControl("p");
                        prod_details.InnerHtml = prom.short_description;
                        oferta_text.Controls.Add(prod_details);
                    }

                    var detailsLink = new HtmlGenericControl("a");
                    detailsLink.Attributes["class"] = "details";
                    detailsLink.Attributes["href"] = Page.GetRouteUrl("ProductDetails", new { id = prom.ID, prodName = prom.name });
                    detailsLink.InnerHtml = "detalii";

                    oferta_details.Controls.Add(oferta_title);
                    oferta_details.Controls.Add(oferta_text);
                    oferta_details.Controls.Add(detailsLink);
                }
                oferta_content.Controls.Add(oferta_details);
            }

            var promotie = new View();
            promotie.Controls.Add(oferta_content);
            OfertaContentMV.Views.Add(promotie);

            var buttonpager = new LinkButton() { ID = Convert.ToString(i), Text = Convert.ToString(i++) };
            buttonpager.Click += new EventHandler(buttonpager_Click);
            OfertaPager.Controls.Add(buttonpager);
        }
        if (promotii.Count > 0)
        {
            buttonpager_Click(OfertaPager.Controls[0], e);
        }
    }
    private void buttonpager_Click(object sender, System.EventArgs e)
    {
        var buttonid = Convert.ToInt32(((LinkButton)sender).ID);
        OfertaContentMV.ActiveViewIndex = buttonid - 1;
        EnablePagerButton(buttonid);
    }

    private void EnablePagerButton(int idxButton)
    {
        var buttons = OfertaPager.Controls;
        foreach (LinkButton b in buttons)
        {
            if (b.ID.Equals(Convert.ToString(idxButton)))
            {
                b.Enabled = false;
                b.Text = "<span class=\"current\">" + b.ID + "</span>";
            }
            else
            {
                b.Enabled = true;
                b.Text = b.ID;
            }
        }
    }
}